package com.selfdriving.adapter;

import java.util.List;

import com.selfdriving.R;
import com.selfdriving.bean.Feature;
import com.selfdriving.utils.PhotoUtil;
import com.selfdriving.utils.StringUtil;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class FeatureListAdapter extends BaseAdapter {
	private Context context;

	private List<Feature> features;

	private HolderView holderView;

	public FeatureListAdapter(Context context, List<Feature> features) {
		this.context = context;
		this.features = features;
	}

	@Override
	public int getCount() {
		return features.size();
	}

	@Override
	public Object getItem(int poistion) {
		return features.get(poistion);
	}

	@Override
	public long getItemId(int poistion) {
		return poistion;
	}

	@Override
	public View getView(int poistion, View view, ViewGroup arg2) {
		Feature feature = features.get(poistion);
		if (view == null) {
			holderView = new HolderView();
			view = ((LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
					.inflate(R.layout.popular_list_item, null);
			view.setTag(holderView);
		} else {
			holderView = (HolderView) view.getTag();
		}
		holderView.name = (TextView) view.findViewById(R.id.tvName);
		holderView.num = (TextView) view.findViewById(R.id.tvNum);
		holderView.icon =(ImageView)view.findViewById(R.id.ivIcon);
		if(!StringUtil.isNullOrEmpty(feature.name)){
			holderView.name.setText(poistion+1+"   "+feature.name);
		}
		
		if(!StringUtil.isNullOrEmpty(feature.popularNum)){
			holderView.num.setText(feature.popularNum);
		}
		if(poistion==0){
			holderView.icon.setImageBitmap(PhotoUtil.readBitmap(context, R.drawable.icon_rank_five));
		}else if(poistion==1){
			holderView.icon.setImageBitmap(PhotoUtil.readBitmap(context, R.drawable.icon_rank_four));
		}else if(poistion==2){
			holderView.icon.setImageBitmap(PhotoUtil.readBitmap(context, R.drawable.icon_rank_three));
		}
		else if(poistion==3){
			holderView.icon.setImageBitmap(PhotoUtil.readBitmap(context, R.drawable.icon_rank_two));
		}
		else if(poistion==4){
			holderView.icon.setImageBitmap(PhotoUtil.readBitmap(context, R.drawable.icon_rank_one));
		}
		return view;
	}

	private class HolderView {
		private ImageView icon;

		private TextView name;

		private TextView num;
	}

}
